/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import http from '@ohos.net.http';
import prompt from '@system.prompt';
import router from '@system.router';

export default {
    data: {
        name: '1',
        phone:'0',
        userId:'0',
        role: '学生',
        sex : 1,
        age : '0',
        lampSN:'',
        isDisabled:'true',
        token: ''
    },

    props: {
         isDisable: {
                default: true
             },
         lampsn: {
             default: ''
         },
        result: {
            default:''
        },

        } ,

    lampText(){
        this.lampSN =this.lampsn;
        console.info("传sn号-----" + this.lampSN);
        console.info("传sn号-----" + this.token);
        console.info("传sn号-----" + this.result.token);
    },

    onInit() {
        this.token =this.result.token;
        this.name =this.result.username;
        this.phone = this.result.phone;
        this.userId = this.result.userId;
        this.sex = this.result.sex;
        this.age = this.result.age;
        this.role = this.result.role;
        console.info("perfo----isdiable"+ this.isDisabled)
        this.$watch('isDisable', 'onChangeisDisabled');//给pSelected设置数据变化监听，$watch 监控数据改变
        this.$watch('lampsn', 'lampText');//给pSelected设置数据变化监听，$watch 监控数据改变

    },



    onChangeisDisabled(){

        this.isDisabled = this.isDisable;
        console.info("person---" + this.isDisable)
    },

    fetch(){
        let httpRequest = http.createHttp();
        // 用于订阅http响应头，此接口会比request请求先返回。可以根据业务需要订阅此消息
        httpRequest.on('headerReceive', (err, data) => {

        });
        httpRequest.request(
        // 填写http请求的url地址，可以带参数也可以不带参数。URL地址需要开发者自定义。GET请求的参数可以在extraData中指定
            "http://8.142.187.137:8888/user/bandDevice",
            {
                method: 'POST', // 可选，默认为“GET”
                // 开发者根据自身业务需要添加header字段
                header: {
                    'Content-Type': 'application/json',
                    'token': this.token
                },
                // 当使用POST请求时此字段用于传递内容
                extraData: {'deviceSn': this.lampSN},
                readTimeout: 60000, // 可选，默认为60000ms
                connectTimeout: 60000 // 可选，默认为60000ms
            },(err, data) => {
            if (!err) {
                // data.result为http响应内容，可根据业务需要进行解析
                var result = JSON.parse(data.result);
                console.info('Result:' + data.result);
                console.info('code:' + data.responseCode);
                // data.header为http响应头，可根据业务需要进行解析
                console.info('header:' + data.header);
                if(result["code"] == '200'){
                    prompt.showToast({
                        message: result.msg
                    })
                this.isDisabled = true;


                }else if(result["code"] == '400'){
                    prompt.showToast({
                        message: result.msg
                    })
                }

            } else {
                console.info('error:' + err.data);
            }
        }
        );
    },







    getLampID(e) {

        this.lampSN = e.value;
        console.info("lampSN=" + this.lampSN);
    },
    onBinding() {

        this.fetch();
        console.info("绑定设备---");
    }
}
